<!-- 模本文件 -->
<template>
  <el-container class="container" v-loading="loading" element-loading-text="拼命加载中...">
    <el-header height="68px">
      <!-- logo -->
      <div class="logo"><img src="@/assets/logo@2x.png" alt="" width="110px" height="48px"></div>

      <!-- 用户资料区域 -->
      <div class="user">
        <!-- 头像 -->
        <img :src="userInfo.user_pic" alt="">

        <!-- 个人中心 -->
        <div>
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              个人中心<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" style="margin-top: 30px;background: #24262E;">
              <el-dropdown-item icon="el-icon-s-operation" @click.native="toPush('basicInfo')">基本资料</el-dropdown-item>
              <el-dropdown-item icon="el-icon-camera" @click.native="toPush('replaceHead')">更换头像</el-dropdown-item>
              <el-dropdown-item icon="el-icon-key" @click.native="toPush('resetPassword')">重置密码</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>

        <!-- 退出 -->
        <div class="LogOut" @click="LogOutisShow = true">
          <img src="@/assets/退出@2x.png" alt="" width="24px" height="24px">
          <span>退出</span>
        </div>

        <!-- 退出提示框 -->
        <el-dialog title="title" :visible.sync="LogOutisShow" width="25%">
          <div slot="title" class="header-title">
            <span class="title-name">
              <i class="el-icon-warning" style="color: #4f81ff;margin-right:10px;font-size: 24px;"></i>
              <span
                style="font-family: PingFangSC-Medium;font-size: 16px;color: rgba(0,0,0,0.90);line-height: 24px;font-weight: 500;">提示</span>
            </span>
          </div>
          <span style="margin-left: 30px;">确定要退出登录吗？</span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="LogOutisShow = false" size="medium">取消</el-button>
            <el-button type="primary" @click="LogOut" size="medium" style="background-color: #4f81ff;">确认</el-button>
          </span>
        </el-dialog>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="254px">
        <el-row class="tac">
          <el-col :span="24">
            <!-- :unique-opened="true" 是否只保持一个子菜单的展开  取值 boolean  -->
            <el-menu :default-active="NavActive" class="el-menu-vertical-demo" background-color="#24262e"
              text-color="#fff" :unique-opened="false" router>
              <!-- 显示个人信息 -->
              <div style="color: white;" class="info" :style="{ height: infoHeight }">
                <img :src="userInfo.user_pic" alt="" width="36px" height="36px">
                <span>欢迎 <span>{{ userInfo.username }}</span> </span>
              </div>
              <!-- 首页 -->
              <el-menu-item index="index">
                <i class="el-icon-s-home"></i>
                <span slot="title">首页</span>
              </el-menu-item>

              <!-- 文章管理 -->
              <el-submenu index="2">
                <template slot="title">
                  <i class="el-icon-s-order"></i>
                  <span>文章管理</span>
                </template>
                <el-menu-item-group>
                  <el-menu-item index="assort">
                    <i class="el-icon-s-data"></i>文章分类
                  </el-menu-item>
                </el-menu-item-group>
                <el-menu-item-group>
                  <el-menu-item index="tabulation">
                    <i class="el-icon-document-copy"></i>文章列表
                  </el-menu-item>
                </el-menu-item-group>
                <el-menu-item-group>
                  <el-menu-item index="publish" v-show="false">
                    <i class="el-icon-star-off"></i>发表文章
                  </el-menu-item>
                </el-menu-item-group>
              </el-submenu>

              <!-- 个人中心 -->
              <el-submenu index="3">
                <template slot="title">
                  <i class="el-icon-user-solid"></i>
                  <span>个人中心</span>
                </template>
                <el-menu-item-group>
                  <el-menu-item index="basicInfo">
                    <i class="el-icon-s-operation"></i>基本资料
                  </el-menu-item>
                </el-menu-item-group>
                <el-menu-item-group>
                  <el-menu-item index="replaceHead">
                    <i class="el-icon-camera"></i>更换头像</el-menu-item>
                </el-menu-item-group>
                <el-menu-item-group>
                  <el-menu-item index="resetPassword">
                    <i class="el-icon-key"></i>重置密码</el-menu-item>
                </el-menu-item-group>
              </el-submenu>
            </el-menu>
          </el-col>
        </el-row>
      </el-aside>

      <el-container>
        <!-- 主要内容 -->
        <el-main>
          <!-- 二级路由显示位置 -->
          <div class="el-main-box">
            <keep-alive :include="['articlePublish']">
              <router-view :IndexisShow="IndexisShow" @setIndexisSHow="setIndexisSHow" @setUserInfo="setUserInfo"
                @setLoading="setLoading"></router-view>
            </keep-alive>
          </div>
        </el-main>

        <!-- 底部栏 -->
        <el-footer height="40px">
          <el-row :gutter="20">
            <el-col :span="24">
              <div class="grid-content bg-purple">ww.boxuegu.com - 博学谷</div>
            </el-col>
          </el-row>
        </el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import store from '@/store'
import { GetUser } from '@/api/user'
export default {
  name: 'DailyIndex',
  async created () {
    store.commit('user/NewSetToken', localStorage.getItem('token'))
    const res = await GetUser()
    this.userInfo = res.data.data
    localStorage.setItem('userInfo', JSON.stringify(res.data.data))
    store.commit('user/setUserInfo', res.data.data)
    // console.log(this.userInfo)
    // console.log(res.data.data)
  },
  data () {
    return {
      // 头像
      IndexisShow: '',
      infoHeight: '',
      LogOutisShow: false,
      userInfo: '',
      NavActive: 'index',
      loading: false
    }
  },
  methods: {
    // 控制是否显示用户名模块
    setIndexisSHow (newValue) {
      this.IndexisShow = newValue
    },
    setUserInfo (newValue) {
      this.userInfo = newValue
    },
    // 控制加载显示
    setLoading (newValue) {
      this.loading = newValue
    },
    // 退出功能
    LogOut () {
      // 删除token
      store.commit('user/LogOut')
      // 关闭弹框
      this.LogOutisShow = false
      // 将路由跳转至登录
      this.$router.push('/login')
    },
    // 跳转路由
    toPush (data) {
      this.$router.push(`/${data}`)
    },
    // 监听路由的变化，对应菜单高亮显示
    handleSelect (index) {
      // console.log(index)
      if (index !== 'publish') {
        this.NavActive = index
      }
    }
  },
  watch: {
    IndexisShow: { // 监听的属性
      deep: true, // 深度监视   监视对象内所有属性
      handler (newValue) {
        if (newValue) {
          // console.log(newValue)
          this.infoHeight = '96px'
        } else {
          this.infoHeight = '0px'
        }
      }
    },
    userInfo: { // 监听的属性
      deep: true, // 深度监视   监视对象内所有属性
      handler (newValue) {
        this.avatar = this.userInfo.user_pic
      }
    },
    // 监听路由变化
    $route (index) {
      const path = index.path.substr(1)
      this.handleSelect(path)
    }
  }
}
</script>

<style lang="less" scoped>
// 将弹出框水平垂直居中显示
/deep/ .el-dialog {
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  margin: 0px !important;
}

.title-name {
  display: flex;
  align-content: center;
  color: red;
}

.el-dialog__headerbtn .el-dialog__close {
  color: #1a1a1a;
}

.iconfont {
  width: 24px;
  height: 24px;
  margin-right: 10px;
  color: #FFFFFF;
}

.container {
  height: 100vh;
}

.el-header {
  position: relative;
  display: flex;
  align-content: center;
  justify-content: space-between;
  background: #24262E;
  color: #ffffff;
  box-shadow: 0px 1px 4px 0px rgba(21, 34, 50, 0.8);

  .logo {
    margin-top: 10px;
  }

  .user {
    display: flex;
    align-items: center;

    .el-dropdown-link {
      cursor: pointer;
      font-family: PingFangSC-Medium;
      font-size: 14px;
      color: #FFFFFF;
      letter-spacing: 0;
      line-height: 20px;
      font-weight: 500;
    }

    img {
      margin-right: 12px;
      width: 36px;
      height: 36px;
      border-radius: 50%;
    }

    .el-icon--right {
      margin-left: 10px;
    }

    .LogOut {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-left: 20px;
      cursor: pointer;

      img:nth-child(1) {
        width: 24px;
        height: 24px;
      }
    }
  }
}

.el-aside {
  background: #24262E;
  color: #333;
  text-align: center;

  .el-menu-item,
  .el-submenu {
    color: #fff;
    text-align: left;
  }

  // 去除默认的边框线
  .el-menu {
    border: none;
    padding: 0 8px;
  }

  .el-menu-item,
  .el-submenu /deep/ .el-submenu__title {
    height: 44px;
    line-height: 44px;
  }

  // 修改二级菜单内的间距
  /deep/ .el-menu-item-group__title {
    padding: 0;
  }

  // 设置选中的背景颜色跟字体颜色
  .el-menu-item.is-active {
    background: #292C38 !important;
    color: #4F81FF;
  }

}

.el-main {
  padding: 24px;
  background-color: #f5f6fa;
  color: #333;
  // height: 300px;
  // overflow: auto;
}

.el-footer {
  padding: 0 24px;
  line-height: 40px;
  text-align: center;
  background-color: #f5f6fa;

  .bg-purple {
    background: #FFFFFF;
    font-family: PingFangSC-Regular;
    font-size: 12px;
    color: #131523;
    letter-spacing: 0;
    text-align: center;
    font-weight: 400;
  }
}

.info {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 0px;
  line-height: 96px;
  color: white;
  background: #101219;
  transition: all linear .2s;

  img {
    margin-right: 12px;
    border-radius: 50%;
  }

  span {
    font-family: PingFangSC-Regular;
    font-size: 14px;
    color: #FFFFFF;
    letter-spacing: 0;
    line-height: 22px;
    font-weight: 400;
  }
}

/deep/ .el-dropdown-menu__item {
  color: #fff !important;
}

/deep/ .el-dropdown-menu__item:hover {
  background: #2E3240 !important;
}

/deep/ .popper__arrow::after {
  border-bottom-color: #24262E !important;
}
</style>
